package com.example.statistics.ui.purchase

import android.os.Bundle
import com.example.statistics.R
import com.example.statistics.bean.TaxFeeBean
import com.example.statistics.common.BaseActivity
import com.example.statistics.ex.*
import com.example.statistics.net.UrlConfig
import com.example.statistics.utils.Constants
import com.example.statistics.utils.activityFinish
import com.example.statistics.utils.currencyMay
import com.jhj.httplibrary.model.HttpParams
import com.example.statistics.ex.afterTextChangedListener
import com.example.statistics.ex.textStringToNumber
import kotlinx.android.synthetic.main.activity_tax_fee.*
import kotlinx.android.synthetic.main.activity_tax_fee.tv_tariff
import kotlinx.android.synthetic.main.activity_tax_fee.tv_tax_difference
import kotlinx.android.synthetic.main.activity_tax_fee.tv_value_added_tax
import org.jetbrains.anko.toast
import java.util.*

class TaxFeeActivity : BaseActivity() {


    private var currency = "$"
    private var tariff = 0.12


    override val title: String
        get() = intent.getStringExtra(Constants.TITLE) ?: "税款"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_tax_fee)
        val data = intent.getSerializableExtra(Constants.DATA) as TaxFeeBean?
        val containerNo = intent.getStringExtra(Constants.CONTAINER_NO)
        val purchaseId = intent.getIntExtra(Constants.PURCHASE_PRODUCT_ID, 0)
        currency = intent.getStringExtra(Constants.TYPE).orEmpty()
        tv_dollar.labelText = currencyMay[currency] + "："
        tariff = if (title.contains("肚")) 0.20 else 0.12

        if (data != null) {
            tv_date.contentText = data.date.goTimeFormat()
            tv_dollar.contentText = data.dollar.toString()
            tv_rate.contentText = data.rate.toString()
            tv_tariff.contentText = data.tariff.toString()
            tv_value_added_tax.contentText = data.valueAddedTax.toString()
            tv_tax_difference.contentText = data.taxDifference.toString()
            tv_total_fee.contentText = data.totalFee.toString()
        } else {
            tv_date.contentText = Date().format()
            tv_dollar.contentText = intent.getDoubleExtra(Constants.AMOUNT, 0.0).toString()
            dataRefresh()
        }

        initView()

        topBarRightText("保存") {
            if (tv_tariff.isBlank() || tv_value_added_tax.isBlank() || tv_total_fee.isBlank()) {
                return@topBarRightText
            }
            if (containerNo.isNullOrBlank()) {
                toast("请补填集装箱号")
                return@topBarRightText
            }

            val params = HttpParams()
            val httpUrl = if (data == null) {
                UrlConfig.PURCHASE_TAX_ADD
            } else {
                params.put("ID" to data.ID)
                UrlConfig.PURCHASE_TAX_UPDATE
            }
            params.put(
                    "containerNo" to containerNo,
                    "date" to tv_date.contentText,
                    "purchaseProductId" to purchaseId,
                    "dollar" to tv_dollar.contentText,
                    "rate" to tv_rate.contentText,
                    "tariff" to tv_tariff.contentText,
                    "valueAddedTax" to tv_value_added_tax.contentText,
                    "taxDifference" to tv_tax_difference.contentText,
                    "totalFee" to tv_total_fee.contentText
            )
            httpPost<TaxFeeBean>(httpUrl, config = {
                httpParams = params
            }) {
                activityFinish(Constants.DATA to it)
            }
        }
    }

    private fun initView() {

        tv_date.onDateClicked {
            dataRefresh()
        }

        tv_tax_difference.afterTextChangedListener { _, _ ->
            if (tv_rate.text.isNullOrBlank()) {
                return@afterTextChangedListener
            }
            tv_total_fee.contentText = tv_tariff.textStringToNumber
                    .addEx(tv_value_added_tax.textStringToNumber)
                    .addEx(tv_tax_difference.textStringToNumber).toString()
        }

        tv_tariff.afterTextChangedListener { _, _ ->
            if (tv_rate.text.isNullOrBlank()) {
                return@afterTextChangedListener
            }
            val rate = tv_rate.text.toString().toDouble()
            tv_value_added_tax.contentText = (tv_tariff.textStringToNumber
                    .addEx(tv_dollar.contentText?.toDoubleOrNull().mulEx(rate)))
                    .mulEx(0.09).toString()

            tv_total_fee.contentText = tv_tariff.textStringToNumber
                    .addEx(tv_value_added_tax.textStringToNumber)
                    .addEx(tv_tax_difference.textStringToNumber).toString()

        }

        tv_value_added_tax.afterTextChangedListener { editable, _ ->
            if (tv_rate.text.isNullOrBlank()) {
                return@afterTextChangedListener
            }
            tv_total_fee.contentText = tv_tariff.textStringToNumber
                    .addEx(tv_value_added_tax.textStringToNumber)
                    .addEx(tv_tax_difference.textStringToNumber).toString()
        }
    }

    private fun dataRefresh() {

        val type = if (currency == "$") "dollar" else if (currency == "€") "euro" else "rmb"
        if (type == "rmb") {
            tax(1.0)
        } else {
            httpGet<Double>(UrlConfig.PURCHASE_RATE_DATA, config = {
                isShowDialog = true
                listParams = listOf("date" to tv_date.contentText?.substring(0, 7), "type" to type)
            }) {
                tax(it)
            }
        }
        
    }

    private fun tax(it: Double?) {
        val dollar = tv_dollar.contentText?.toDoubleOrNull()
        tv_rate.contentText = it.toString()
        val tariffFee = dollar.mulEx(it).mulEx(tariff).toDouble()
        val valueAddedTax = (tariffFee.addEx(dollar.mulEx(it))).mulEx(0.09).toDouble()
        tv_tariff.contentText = tariffFee.toString()
        tv_value_added_tax.contentText = valueAddedTax.toString()
    }
}